goog.provide('GLL.Sprite.Anim');

goog.require('lime');
goog.require('lime.animation.KeyframeAnimation');
goog.require('lime.fill.Frame'); 

/*
|---1----|----2---|----3---|----1---|
^-----------loop-----------^
一个循环是指从 第一帧开始到最后一帧结束的时间
@param width
@param height
@param key_x
@param key_y
@param _100ppl pixal per loop.
@param spl second per loop.
*/
GLL.Sprite.Anim= function(file, width, height, key_x, key_y, _100ppl, spl)
{
	lime.animation.KeyframeAnimation.call(this);
	
	this._frameWidth = width / key_x;
	this._frameHeight = height / key_y;
	this._loadFrames(file, width, height, key_x, key_y);
	this._frameCnt = key_x * key_y;

	this._fp100p = 0;//moving animation
	this._spf = 0; //stand animation
	
	if(_100ppl > 0){
		this._fp100p = this._frameCnt / _100ppl; //frame per 100pixals
	}else{
		this._spf = spl / this._frameCnt; //second per frame
	}
	
	this.setSpeed(0);
};

goog.inherits(GLL.Sprite.Anim, lime.animation.KeyframeAnimation);

/*@private
*/
GLL.Sprite.Anim.prototype._loadFrames = function(filename, pic_width, pic_height, frame_x, frame_y)
{
	var frame_height = pic_height / frame_y;
	var frame_width = pic_width / frame_x;

	for(var y = 0; y < frame_y; y++)
	{
		var frame_top = y * frame_height;
		for(var x = 0; x < frame_x; x++)
		{
			var frame_left = x * frame_width;
			var frame = new lime.fill.Frame(filename, frame_left, frame_top, frame_width, frame_height);
			this.addFrame(frame);
		}
	}
}

/*
@speed 
*/
GLL.Sprite.Anim.prototype.setSpeed = function(_100pps)
{
	if(this._fp100p == 0)//stand anim. careless of speed
	{
		this.setDelay(this._spf);
		return;
	}
	if(_100pps <=0){
		this.stop();//no speed, no animation
		return;
	}
	var fps = _100pps * this._fp100p;
	var spf = 1 / fps;
	this.setDelay(spf);
	this.play();
	return this;
};